package com.ruoyi.hr.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.mapper.OrgsMapper;
import com.ruoyi.hr.mapper.ProcedureMapper;
import com.ruoyi.hr.service.OrgsService;
import com.ruoyi.hr.util.TreeBulid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class OrgsServiceImpl implements OrgsService {

    @Autowired
    OrgsMapper orgsMapper;

    @Autowired
    ProcedureMapper procedureMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Map<String, Object> findOrgs(Map map) {
        String type = map.get("type").toString();
        StringBuffer sql = new StringBuffer();

        Map<String, Object> OrgsMap = new HashMap<String, Object>();

        if (type.equals("comp")) {
            sql.append("oSP_chartComp '" + map.get("term").toString() + "',100," + map.get("id"));
            procedureMapper.executeProcedure(sql.toString());
            OrgsMap.put("orgsData", TreeBulid.build(orgsMapper.findOrgsComp(map)));
            OrgsMap.put("onclickType", "dep");
            OrgsMap.put("Table", orgsMapper.compTable());
        } else if (type.equals("dep")) {
            sql.append("oSP_chartDep '" + map.get("term").toString() + "'," + map.get("id") + ",100");
            procedureMapper.executeProcedure(sql.toString());
            OrgsMap.put("orgsData", TreeBulid.build(orgsMapper.findOrgsDep(map)));
            OrgsMap.put("onclickType", "job");
            OrgsMap.put("Table", orgsMapper.depTable());
        } else if (type.equals("job")) {
            sql.append("oSP_chartJob '" + map.get("term").toString() + "'," + map.get("id") + ",100");
            procedureMapper.ExProcedure(sql.toString());
            OrgsMap.put("orgsData", TreeBulid.build(orgsMapper.findOrgsJob(map)));
            OrgsMap.put("onclickType", "emp");
            OrgsMap.put("Table", orgsMapper.jobTable());
        } else if (type.equals("emp")) {
            sql.append("oSP_chartEmp '" + map.get("term").toString() + "'," + map.get("id") + ",100");
            procedureMapper.executeProcedure(sql.toString());
            OrgsMap.put("orgsData", TreeBulid.build(orgsMapper.findOrgsEmp(map)));
            OrgsMap.put("onclickType", "over");
            OrgsMap.put("Table", orgsMapper.empTable());
        }
        return OrgsMap;
    }
}
